package com.jxc.action.qxgl;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VUser;
import com.jxc.service.OnlineServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.IpUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/")
@Action(value = "loginAction", results = {
		@Result(name = "login", location = "/login.jsp"),
		@Result(name = "logout", location = "/index.html", type = "redirect"),
		@Result(name = "index", location = "/index.html" , type = "redirect") })
public class LoginAction extends BaseAction implements ModelDriven<VUser> {

	private final static Logger logger = Logger.getLogger(LoginAction.class);

	private VUser vUser = new VUser();

	@Autowired
	private UserServiceI userService;
	@Autowired
	private OnlineServiceI onlineService;
	public String login() {
		Integer i = userService.login(vUser);
		if(i==1){
			VUser u = userService.login1(vUser);
			if (u != null) {
				SessionInfo sessionInfo = new SessionInfo();
				sessionInfo.setUserId(u.getId());
				sessionInfo.setLoginName(u.getUsername());
				sessionInfo.setLoginPassword(u.getPassword());
				sessionInfo.setIp(IpUtil.getIpAddr(ServletActionContext
						.getRequest()));
				ServletActionContext.getRequest().getSession().setAttribute(
						ResourceUtil.getSessionInfoName(), sessionInfo);
				sessionInfo.setAuthIds(u.getAuthIds());
				sessionInfo.setAuthNames(u.getAuthNames());
				sessionInfo.setRoleIds(String.valueOf(u.getRoid()));
				sessionInfo.setRoleNames(u.getRoname());
				sessionInfo.setAuthUrls(u.getAuthUrls());
				sessionInfo.setRepertoryIds(String.valueOf(u.getReid()));
				sessionInfo.setType(u.getUserType());
				onlineService.saveOrUpdateTonlineByLoginNameAndIp(sessionInfo.getLoginName(),sessionInfo.getIp(),sessionInfo.getUserId());
				try {
					ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
					ServletActionContext.getResponse().getWriter().write("false");
					ServletActionContext.getResponse().getWriter().flush();
					ServletActionContext.getResponse().getWriter().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				logger.info("登录成功！");
				return null;
			} else {
				try {
					ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
					ServletActionContext.getResponse().getWriter().write("对不起,登录出现异常");
					ServletActionContext.getResponse().getWriter().flush();
					ServletActionContext.getResponse().getWriter().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				logger.info("登录失败！");
				return null;
			}
		}else if(i==0){
			try {
				ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
				ServletActionContext.getResponse().getWriter().write("您输入的密码和用户名不匹配，请重新输入");
				ServletActionContext.getResponse().getWriter().flush();
				ServletActionContext.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(i==10){
			try {
				ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
				ServletActionContext.getResponse().getWriter().write("对不起,用户未到启用时间");
				ServletActionContext.getResponse().getWriter().flush();
				ServletActionContext.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(i==20){
			try {
				ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
				ServletActionContext.getResponse().getWriter().write("用户已过期,请联系管理员休整相关事宜");
				ServletActionContext.getResponse().getWriter().flush();
				ServletActionContext.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(i==30){
			try {
				ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
				ServletActionContext.getResponse().getWriter().write("对不起,用户已失效");
				ServletActionContext.getResponse().getWriter().flush();
				ServletActionContext.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
 
//}else{
//	return 30;//用户已失效
//}
//}else{
//return 20;//用户已过期,请联系管理员休整相关事宜
//}
//
//}else{
//return 10;//用户未到启用时间
//}
//}else{
//return 0;//用户名和密码不匹配的时候
//}
	public String logout() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		onlineService.deleteTonlineByLoginNameAndIp(sessionInfo.getLoginName(), sessionInfo.getIp());
		ServletActionContext.getRequest().getSession().invalidate();
		logger.info("登出成功！");
		return "logout";
	}

	@Override
	public VUser getModel() {
		return vUser;
	}
}
